// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require("base");
    var router = require("../hash/pageRouter");
    var css_this = require("./createTask.css");
    var dialog = require("dialog");
    var validate = require("validate");
    var databind = require("databind");
    var tools = require("tools");
    var pageIndex = 1,
        pageSize = 10,
        totalPageCount = 1,
        totalRecordCount = 0; //分页初始化
    var imgSrc;
    var recieveNoticeId;
    var noticeState;
    var left;
    var financeId;
    var init = function(page){
    	left = base.getUrlParam("left");
    	if(left==null){
			left=1;
		}
		initEvent();
		router.init();
    };
	var initEvent = function(){
		//若未登录，则返回登陆页面
		if(base.getData("sid")==null||base.getData("sid")==""||base.getData("sid")==undefined){
			top.location.href="../login/login.html";
		}
		
		$(".createOrderTable .confirm").unbind("click").click(function(){
			var $this = $(this);
			databind.submitDom($this.parents("tr"), function(){
				base.alert("添加成功！");
				databind.disableInput($this.parents("tr"));
				$this.parents("tr").find("td:last").html("");
			});
		});
		
		$("#createTask").on("click", ".addBulkstockItem", function(){
			var str = '<tr><td><select class="bulkstocks" name="bulkstockInfoId"></select></td><td><input type="text" name="quantity" /></td><td><input type="text" name="weight" /></td><td><input type="text" name="volume" /></td><td><input type="text" name="originPlace" /></td><td><a href="javascript:void(0);" class="delete">删除</a></td></tr>';
			$("#createTask .bulkstockInfoTable").append(str);
			tools.loadBulkstockData($("#createTask .bulkstocks:last"));
		});
		
		$("#createTask").on("click", ".addBookingItem", function(){
			var str = '<tr style="background: none;" action="createFinanceContainerBill"><td><input type="hidden" name="recieveNoticeId" value="' + recieveNoticeId + '"/><select name="model"><option>10</option><option>20</option><option>40</option><option>48</option></select></td><td><select name="dimension"><option>HQ</option><option>GP</option><option>RH</option></select></td><td><input type="text" name="emptyWeight" readonly="readonly" value="F" /></td><td><input type="text" name="quantity" /></td><td><input type="checkbox" class="soc" /><input value="false" type="hidden" name="soc"/></td><td><a href="javascript:void(0);" class="confirm">确认</a> <a class="concel" href="javascript:void(0);">取消</a></td></tr>';
			$("#createTask .createOrderTable").append(str);
			
			$(".soc").unbind("click").click(function(){
				var $this = $(this);
				if ($this.prop("checked")) {
					$this.parent().find("input[name=soc]").val("true");
				} else {
					$this.parent().find("input[name=soc]").val("false");
				}
			});
			
			$(".createOrderTable .confirm").unbind("click").click(function(){
				var $this = $(this);
				databind.submitDom($this.parents("tr"), function(){
					base.alert(1,"添加成功！");
					databind.disableInput($this.parents("tr"));
					$this.parents("tr").find("td:last").html("");
				});
			});
		});
		
		$(".bulkstockInfoTable").on("click", ".delete", function(){
			$(this).parents("tr").remove();
		});
		
		$("#createTask .submitBtn").unbind("click").click(function(){
			databind.submitDom($("#createTask"), function(data){
				recieveNoticeId = data.datas[0].recieveNoticeId;
				base.alert(1, "新增成功！");
				databind.disableInput($(".bulkstockInfoTable"));
				$(".bulkstockInfoTable").find(".delete").remove();
				$(".createOrderTable").removeClass("none");
				$(".addBookingItem").removeClass("none");
				$(".addBulkstockItem").remove();
				$("#createTask .submitBtn").remove();
				$("#createTask .finishBtn").removeClass("none");
			});
		});
		
		$("#createTask .finishBtn").unbind("click").click(function(){
			router.route("newTaskList/newTaskList.html");
		});
		
		tools.loadBulkstockData($("#createTask .bulkstocks:last"));
    };
     
    return {
        init:init
    };
});